package cz.cuni.mff.ufal.volk.command;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

public abstract class Command {

	public final UUID id = UUID.randomUUID();

  /**
   * Returns the command type.
   * @return the command type
   */
  public abstract byte commandType();
  public abstract byte[] getBytes();

  public void writeCommand(OutputStream out) throws IOException {
    byte[] cmdBytes = getBytes();
    if ((cmdBytes.length & 0xff000000) != 0)
      throw new IOException(String.format("Cannot send %d bytes to Nabaztag", cmdBytes.length));
    byte[] ts = new byte[4];
    ts[0] = commandType();
    ts[1] = (byte) ((cmdBytes.length & 0x00ff0000) >> 16);
    ts[2] = (byte) ((cmdBytes.length & 0x0000ff00) >> 8);
    ts[3] = (byte) (cmdBytes.length & 0x000000ff);

    out.write(ts);
    out.write(getBytes());
  }

  @Override
  public String toString() {
    return super.toString();
  }
}
